package net.easyjava.web.controller;

import net.easyjava.base.bean.ArticleBean;
import net.easyjava.base.constants.AlbumConstants;
import net.easyjava.base.constants.CommonConstants;
import net.easyjava.base.manager.ArticleManager;
import net.easyjava.tools.http.HttpTools;
import net.easyjava.tools.http.request.RequestTools;
import net.easyjava.tools.lang.StringTools;
import net.easyjava.tools.model.ModelConstants;
import net.easyjava.web.constants.WebConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by wxnacy on 16/1/1.
 */
@Controller
public class IndexController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private ArticleManager articleManager;
    @Value("${article.address}")
    private String ARTICLE_ADDRESS;

    /**
     * 访问域名进入登陆页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/")
    @ResponseBody
    public ModelAndView index(
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            ArticleBean articleBean = articleManager.findLastOne();
            params.put("article",articleBean);
            params.put("content", articleManager.getArticleContent(articleBean.getPath()));
//            params.put("menu", AlbumConstants.getList(0));
            return new ModelAndView("index.html",params);
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }

    /**
     *
     * @param type  网站访问的类型:       ****静态页面：
     *                                  connection:联系页面
     *
     *                                  ****文章列表：
     *                                  technology:技术文章列表
     * @param pageNum
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/{type}.html")
    @ResponseBody
    public ModelAndView type(
            @PathVariable String type,
            @RequestParam(required = false,defaultValue = CommonConstants.PAGE_NUM+"") int pageNum,
            @RequestParam(required = false,defaultValue = CommonConstants.PAGE_SIZE+"") int pageSize,
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            String url = "";
            if(WebConstants.INDEX_TYPE_CONNECTION.equals(type)){
                url = "web/connection.html";
            }else if(WebConstants.INDEX_TYPE_TECHNOLOGY.equals(type)){
                params.put("page",articleManager.getTechnologyArticleList(pageNum));
                params.put("listUrl", RequestTools.getRealUrl(request));
                url = "web/article/list.html";
            }else{
                message = "地址有误";
                throw new Exception(message);
            }
            return new ModelAndView(url,params);
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }
}
